﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using System.Threading;

namespace SSHTool.RRU_QUERY
{
    public partial class RRUQuery2 : Form
    {

         public RRUQuery2()
        {
            InitializeComponent();
        }
        Tamir.SharpSsh.ShellHelp shell = null;

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (!checkBox1.Checked) txt_pwd.PasswordChar = '*'; else txt_pwd.PasswordChar = (char)0;
        }
        private void btn_Query_Click(object sender, EventArgs e)
        {
            shell = new Tamir.SharpSsh.ShellHelp();

            try
            {
                if (shell.OpenShell(txt_IP.Text.Trim(), txt_user.Text.Trim(), txt_pwd.Text.Trim(), 22))
                {
                    lal_Status.Text = "连接SSH成功";
                    QueryHandle();
                    QueryTPT();
                }
            }
            catch
            {
                lal_Status.Text = "连接失败/文件操作失败";
            }
        }
        /// <summary>
        /// 查询处理
        /// </summary>
        public void QueryHandle()
        {
            string ErrMsg = "";
            List<DDAL_SFP> DDAL_SFPLIST = new List<DDAL_SFP>();
            List<DDAL_SFP_DEV_LOCAL_RADIO_IF> DDAL_SFP_DEV_LOCAL_RADIO_IFLIST = new List<DDAL_SFP_DEV_LOCAL_RADIO_IF>();

            shell = new Tamir.SharpSsh.ShellHelp();
            if (shell.OpenShell(txt_IP.Text.Trim(), txt_user.Text.Trim(), txt_pwd.Text.Trim(), 22))
            {
                #region  /* RRU_FSMF*/

                try
                {
                    string result = RRU_QUERY(ref shell);
                    if (result.IndexOf("No such file or directory") == -1)
                    {

                        DDAL_SFPLIST = new BLL.GET_DDAL_SFP().GetDDAL_SFPmodelList(result);
                        DDAL_SFP_DEV_LOCAL_RADIO_IFLIST = new BLL.GET_DDAL_SFP().GetDDAL_SFP_DEV_LOCAL_RADIO_IF_modelList(result);
                        BindText(DDAL_SFPLIST, DDAL_SFP_DEV_LOCAL_RADIO_IFLIST, "FSMF");
                    }
                    else
                    {
                        ErrMsg = "文件操作失败";
                        lab_FSMF.Text = ErrMsg;//单独的提示
                    }
                }
                catch
                {
                    ErrMsg = "文件操作失败";
                    lab_FSMF.Text = ErrMsg;//单独的提示
                }

                #endregion
                #region /* RRU_FSIH*/
                /*192.168.253.18*/
                shell.Shell("ssh toor4nsn@192.168.253.19");
                try
                {
                    if (shell.SessionLog.IndexOf("Connection refused") > -1 || shell.SessionLog.IndexOf("Connection timed out") > -1)
                    {
                        lal_Status.Text = "192.168.253.19连接失败";
                    }
                    else
                    {
                        #region SSH 登陆 方法
                        Thread.Sleep(1000);
                        string info = shell.SessionLog;
                        if (info.IndexOf("Are you sure you want to continue connecting (yes/no)") > 0)
                        {
                            shell.Shell("yes");
                        }
                        Thread.Sleep(1000);
                        shell.Shell("oZPS0POrRieRtu");
                        #endregion 

                        string result2 = RRU_QUERY(ref shell);
                        if (result2.IndexOf("No such file or directory") == -1)
                        {
                            DDAL_SFPLIST = new BLL.GET_DDAL_SFP().GetDDAL_SFPmodelList(result2);
                            DDAL_SFP_DEV_LOCAL_RADIO_IFLIST = new BLL.GET_DDAL_SFP().GetDDAL_SFP_DEV_LOCAL_RADIO_IF_modelList(result2);
                            BindText(DDAL_SFPLIST, DDAL_SFP_DEV_LOCAL_RADIO_IFLIST, "FSIH");
                        }
                        else
                        {
                            ErrMsg = "文件操作失败";
                            lab_FSIH.Text = ErrMsg;//单独的提示
                        }
                    }
                }
                catch
                {
                    ErrMsg = "文件操作失败";
                    lab_FSIH.Text = ErrMsg;//单独的提示
                }
                #endregion
            }
            else
            {
                lal_Status.Text = txt_IP.Text + "连接失败";
            }
        }
        /// <summary>
        /// 拓扑图
        /// </summary>
        public void QueryTPT()
        {
            string ErrMsg = "";
            string RXstate = string.Empty;
            string err_cnt = "";
            string err_cnt_tot = "";

            try
            {
                if (shell.OpenShell(txt_IP.Text.Trim(), txt_user.Text.Trim(), txt_pwd.Text.Trim(), 22))
                {
                    shell.Shell("cat /sys/class/flexi/rp3_01_link0/rx_sm/state");
                    RXstate = shell.SessionLog;
                    txt_FSMF_RXSTATE.Text = RXstate;

                    shell.Shell("cat /sys/class/flexi/rp3_01_link0/rx_lcv/err_cnt ");
                    err_cnt = shell.SessionLog;
                    txt_FSMF_err_cnt.Text = err_cnt;

                    shell.Shell("cat /sys/class/flexi/rp3_01_link0/rx_lcv/err_cnt_tot");
                    err_cnt_tot = shell.SessionLog;
                    txt_FSMF_err_cnt_tot.Text = err_cnt_tot;

                    if (RXstate.Trim() == "0x3")
                    {
                        lab_lineFSMF.ForeColor = System.Drawing.Color.Green;
                        this.pictureBox3.Image = global::SSHTool.Properties.Resources.RRU_G_IMAGE;
                    }
                    else
                        if (RXstate.Trim() == "0x0")
                        {
                            lab_lineFSMF.ForeColor = System.Drawing.Color.Red;
                            this.pictureBox3.Image = global::SSHTool.Properties.Resources.RRU_R_IMAGE;
                        }
                        else
                        {
                            lab_lineFSMF.ForeColor = System.Drawing.Color.Gray;
                            this.pictureBox3.Image = global::SSHTool.Properties.Resources.RRU_R_IMAGE;
                        }
                }
                else
                {
                    ErrMsg = "误码信息查询失败 FSMF";
                    Lab_FSMF_TPT.Text = ErrMsg;
                }
            }
            catch
            {
                ErrMsg = "误码信息查询失败 FSMF";
                Lab_FSMF_TPT.Text = ErrMsg;
            }

            try
            {
                shell.Shell("ssh toor4nsn@192.168.253.19");
                if (shell.SessionLog.IndexOf("Connection refused") > -1 || shell.SessionLog.IndexOf("Connection timed out") > -1)
                {
                    ErrMsg = "误码信息查询失败 FSIH";
                    Lab_FSIH_TPT.Text = ErrMsg;
                }
                else
                {

                    #region SSH 登陆 方法
                    Thread.Sleep(1000);
                    string info = shell.SessionLog;
                    if (info.IndexOf("Are you sure you want to continue connecting (yes/no)") > 0)
                    {
                        shell.Shell("yes");
                    }
                    Thread.Sleep(1000);
                    shell.Shell("oZPS0POrRieRtu");
                    #endregion 


                    shell.Shell("cat /sys/class/flexi/rp3_01_link0/rx_sm/state");
                    RXstate = shell.SessionLog;
                    txt_FSIH_RXSTATE.Text = RXstate;

                    shell.Shell("cat /sys/class/flexi/rp3_01_link0/rx_lcv/err_cnt ");
                    err_cnt = shell.SessionLog;
                    txt_FSIH_err_cnt.Text = err_cnt;

                    shell.Shell("cat /sys/class/flexi/rp3_01_link0/rx_lcv/err_cnt_tot");
                    err_cnt_tot = shell.SessionLog;
                    txt_FSIH_err_cnt_tot.Text = err_cnt_tot;

                    if (RXstate.Trim() == "0x3")
                    {
                        lab_lineFSIH.ForeColor = System.Drawing.Color.Green;
                    }
                    else
                        if (RXstate.Trim() == "0x0")
                        {
                            lab_lineFSIH.ForeColor = System.Drawing.Color.Red;
                        }
                        else
                        {
                            lab_lineFSIH.ForeColor = System.Drawing.Color.Gray;
                        }
                }
            }
            catch
            {
                ErrMsg = "误码信息查询失败 FSIH";
                Lab_FSIH_TPT.Text = ErrMsg;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="_shell"></param>
        /// <returns></returns>
        private string RRU_QUERY(ref Tamir.SharpSsh.ShellHelp _shell)
        {
            _shell.Shell("cd /flash");
            _shell.Shell("cp -f sfp_tool.tar /");
            _shell.Shell("cd .. ");
            _shell.Shell("tar xvf sfp_tool.tar ");
            Thread.Sleep(1000);
            _shell.Shell("chmod +x sfp_tool ");
            _shell.Shell("./sfp_tool –v ");
            //_shell.Shell("cat sfp_tool");
            return _shell.SessionLog;
        }

        private void BindText(List<DDAL_SFP> _DDAL_SFPLIST, List<DDAL_SFP_DEV_LOCAL_RADIO_IF> _DDAL_SFP_DEV_LOCAL_RADIO_IFLIST, string QueryType)
        {
            //txt_FSMF_DDAL_参数1_SFP_0  FSMF FSIH

            for (int i = 0; i <= 4; i++)
            {
                if (_DDAL_SFPLIST[i] != null)
                {
                    TextBox text1 = (TextBox)new SSHTool.Common.CommonClass().findControl(this, "txt_" + QueryType + "_DDAL_参数1_SFP_" + i.ToString());
                    text1.Text = _DDAL_SFPLIST[i].Vendor;
                }
                if (_DDAL_SFPLIST[i] != null)
                {
                    TextBox text2 = (TextBox)new SSHTool.Common.CommonClass().findControl(this, "txt_" + QueryType + "_DDAL_参数2_SFP_" + i.ToString());
                    text2.Text = _DDAL_SFPLIST[i].Serial;
                }
                if (_DDAL_SFPLIST[i] != null)
                {
                    TextBox text3 = (TextBox)new SSHTool.Common.CommonClass().findControl(this, "txt_" + QueryType + "_DDAL_参数3_SFP_" + i.ToString());
                    text3.Text = _DDAL_SFPLIST[i].Bitrate;
                }
                if (_DDAL_SFPLIST[i] != null)
                {
                    TextBox text4 = (TextBox)new SSHTool.Common.CommonClass().findControl(this, "txt_" + QueryType + "_DDAL_参数4_SFP_" + i.ToString());
                    text4.Text = _DDAL_SFPLIST[i].Wavelength;
                }
                if (_DDAL_SFP_DEV_LOCAL_RADIO_IFLIST[i] != null)
                {
                    TextBox text5 = (TextBox)new SSHTool.Common.CommonClass().findControl(this, "txt_" + QueryType + "_DDAL_参数5_SFP_" + i.ToString());
                    text5.Text = _DDAL_SFP_DEV_LOCAL_RADIO_IFLIST[i].Input_power;
                }
                if (_DDAL_SFP_DEV_LOCAL_RADIO_IFLIST[i] != null)
                {
                    TextBox text6 = (TextBox)new SSHTool.Common.CommonClass().findControl(this, "txt_" + QueryType + "_DDAL_参数6_SFP_" + i.ToString());
                    text6.Text = _DDAL_SFP_DEV_LOCAL_RADIO_IFLIST[i].Output_power;
                }

            }

        }

    }
}
